package com.example.medicalapp.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.medicalapp.R;
import com.example.medicalapp.adapter.DoctorDepartmentAdapter;
import com.example.medicalapp.helper.DatabaseHelper;
import com.example.medicalapp.model.Department;
import com.example.medicalapp.util.PreferencesUtil;

import java.util.List;

public class DoctorHomeActivity extends AppCompatActivity {
    private static final String TAG = "DoctorHomeActivity";
    public static final String EXTRA_DEPT_ID = "com.example.medicalapp.EXTRA_DEPT_ID";
    public static final String EXTRA_DEPT_NAME = "com.example.medicalapp.EXTRA_DEPT_NAME";

    private RecyclerView rvDepartments;
    private DoctorDepartmentAdapter adapter;
    private DatabaseHelper dbHelper;
    private PreferencesUtil preferencesUtil;
    private int doctorId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_home);
        Log.d(TAG, "onCreate called");

        // 初始化视图
        initViews();

        // 初始化数据库和偏好设置工具
        dbHelper = new DatabaseHelper(this);
        preferencesUtil = new PreferencesUtil(this);

        // 获取当前登录医生ID
        doctorId = preferencesUtil.getUserId();
        Log.d(TAG, "Current doctor ID: " + doctorId);

        // 加载科室数据
        loadDepartments();

        // 设置底部导航按钮点击事件
        setupNavigationButtons();
    }

    private void initViews() {
        rvDepartments = findViewById(R.id.rv_departments);
        rvDepartments.setLayoutManager(new LinearLayoutManager(this));
        Log.d(TAG, "Views initialized");
    }

    private void loadDepartments() {
        // 从数据库获取所有科室
        List<Department> departments = dbHelper.getAllDepartments();
        Log.d(TAG, "Loaded " + departments.size() + " departments");

        // 设置适配器
        adapter = new DoctorDepartmentAdapter(departments, this::onDepartmentClick);
        rvDepartments.setAdapter(adapter);
    }

    private void setupNavigationButtons() {
        Button btnHome = findViewById(R.id.btn_home);
        Button btnHistory = findViewById(R.id.btn_history);

        // 历史记录按钮点击事件
        btnHistory.setOnClickListener(v -> {
            Log.d(TAG, "History button clicked");
            Intent intent = new Intent(DoctorHomeActivity.this, DoctorHistoryActivity.class);
            startActivity(intent);
        });
    }

    // 科室点击事件处理
    private void onDepartmentClick(Department department) {
        Log.d(TAG, "Department clicked: " + department.getName() + " (ID: " + department.getId() + ")");

        // 跳转到科室患者列表页面，并传递科室信息
        Intent intent = new Intent(this, DoctorDepartmentActivity.class);
        intent.putExtra(EXTRA_DEPT_ID, department.getId());
        intent.putExtra(EXTRA_DEPT_NAME, department.getName());
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭数据库连接
        if (dbHelper != null) {
            dbHelper.close();
        }
        Log.d(TAG, "onDestroy called");
    }
}